//
//  VisitorLoginView.m
//  Weibo-OC
//
//  Created by 焦林生 on 15/11/25.
//  Copyright © 2015年 cn.baidu. All rights reserved.
//

#import "VisitorLoginView.h"
#import "UIImageView+Extension.h"
#import "Masonry.h"

@interface VisitorLoginView()
//图标图片
@property (nonatomic, weak) UIImageView *iconView;
//小房子
@property (nonatomic, weak) UIImageView *homeIconView;
//描述文字
@property (nonatomic, weak) UILabel *messageLabel;

//遮罩图层
@property (nonatomic, weak) UIImageView *maskIconView;
@end

@implementation VisitorLoginView

- (void)setupInfo:(NSString *)imageName title:(NSString *)title {

    //
    if (imageName == nil) {
        //
        [self startAnim];
        return ;
    }
    self.iconView.image = [UIImage imageNamed:imageName];
    
    self.messageLabel.text = title;
    
    //隐藏小房子
    self.homeIconView.hidden = true;
    //将遮罩图像移动到底层
    [self sendSubviewToBack:self.maskIconView];
    
}

//开启首页转轮动画
- (void)startAnim {

    CABasicAnimation *anim = [[CABasicAnimation alloc]init];
    anim.keyPath = @"transform.rotation";
    anim.toValue = [NSNumber numberWithFloat:2 * M_PI];
    anim.repeatCount = MAXFLOAT;
    anim.duration = 20;
    
    //在view不断切换中  保证动画不停
    anim.removedOnCompletion = false;
    
    //添加到图层
    [self.iconView.layer addAnimation:anim forKey:nil];
    
}

#pragma mark 设置界面
// initWithFrame 是 UIView 的指定构造函数
// 使用纯代码开发使用的
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self setupUI];
    }
    return self;
}

// initWithCoder - 使用 SB & XIB 开发加载的函数
// 使用 sb 开始的入口
- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        
        [self setupUI];
    }
    return self;
}

//设置界面
- (void)setupUI {

    //1 添加图标图片
    UIImageView *icon = [UIImageView initWithImageName:@"visitordiscover_feed_image_smallicon"];
    //获取图标图片
    self.iconView = icon;
    [self addSubview: icon];
    
    //6添加遮罩图层
    UIImageView *mask  = [UIImageView initWithImageName:@"visitordiscover_feed_mask_smallicon"];
    self.maskIconView = mask;
    [self addSubview:mask];
    
    
    //2 添加小房子
    UIImageView *home = [UIImageView initWithImageName:@"visitordiscover_feed_image_house"];
    self.homeIconView = home;
    [self addSubview:home];
    
    
    //3 添加描述文字
    UILabel *message = [UILabel initWithTitle:@"关注一些人，回这里看看有什么惊喜" fontSize:14 color:[UIColor darkGrayColor] screenInset:5];
    self.messageLabel = message;
    [self addSubview:message];
    
    
    //4 添加注册按钮
    UIButton *registerButton = [UIButton title:@"注册"
                                         color:[UIColor orangeColor]
                                         fontSize:14                                                    backImageName:@"common_button_white_disable"];
    self.registerButton = registerButton;
    [self addSubview:registerButton];

    
    //5添加登录按钮
    UIButton *login = [UIButton title:@"登录" color:[UIColor darkGrayColor] fontSize:14 backImageName:@"common_button_white_disable"];
    self.loginButton = login;
    [self addSubview:login];
    
    
    //2 设置约束
    //1> 图片
    [icon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.mas_centerX);
        make.centerY.equalTo(self.mas_centerY).offset(-60);
    }];
    
    //2> 小房子
    [home mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.equalTo(icon.mas_centerX);
        make.centerY.equalTo(icon.mas_centerY);
    }];
    
    //3> 描述文字
    [message mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(icon.mas_centerX);
        make.top.equalTo(icon.mas_bottom).offset(16);
        make.width.mas_equalTo(224);
        make.height.mas_equalTo(36);
    }];
    
    //4> 注册按钮
    [registerButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(message.mas_left);
        make.top.equalTo(message.mas_bottom).offset(16);
        make.width.mas_equalTo(100);
        make.height.mas_equalTo(36);
    }];
    
    //5> 登录按钮
    
    [login mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(message.mas_right);
        make.top.equalTo(registerButton.mas_top);
        make.width.equalTo(registerButton.mas_width);
        make.height.equalTo(registerButton.mas_height);
    }];
    
    //6> 遮罩图层
    [mask mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mas_top);
        make.left.equalTo(self.mas_left);
        make.right.equalTo(self.mas_right);
        make.bottom.equalTo(registerButton.mas_bottom);
    }];
    
    self.backgroundColor = [UIColor colorWithWhite:237.0 / 255.0 alpha:1.0];
}





@end
